<!DOCTYPE html>
<html lang="zh-Hans">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.2.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/vhuichen.ico">
  <link rel="icon" type="image/png" sizes="32x32" href="/vhuichen.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/vhuichen.ico">
  <link rel="mask-icon" href="/vhuichen.ico" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css" integrity="sha256-AbA177XfpSnFEvgpYu1jMygiLabzPCJCRIBtR5jGc0k=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"vhuichen.github.io","root":"/","images":"/images","scheme":"Pisces","darkmode":false,"version":"8.13.0","exturl":false,"sidebar":{"position":"left","display":"display","padding":18,"offset":12},"copycode":{"enable":false,"style":null},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"Searching...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"}}</script><script src="/js/config.js"></script>

    <meta name="description" content="0x00 内存分类根据不同 Section 可以将内存分为  代码段 .text 已初始化数据段 .data 未初始化数据段 .bss 堆 heap  栈 stack    根据内存能否被系统回收，可以分为  Clean Memory Dirty Memory  Clean Memory内存紧张时可以被覆盖，下次需要使用时，触发缺页中断，然后从磁盘加载到内存 （Page In）  system f">
<meta property="og:type" content="article">
<meta property="og:title" content="iOS内存管理机制">
<meta property="og:url" content="https://vhuichen.github.io/2021/04/iOS/%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E6%9C%BA%E5%88%B6/%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E6%9C%BA%E5%88%B6/index.html">
<meta property="og:site_name" content="vhuichen">
<meta property="og:description" content="0x00 内存分类根据不同 Section 可以将内存分为  代码段 .text 已初始化数据段 .data 未初始化数据段 .bss 堆 heap  栈 stack    根据内存能否被系统回收，可以分为  Clean Memory Dirty Memory  Clean Memory内存紧张时可以被覆盖，下次需要使用时，触发缺页中断，然后从磁盘加载到内存 （Page In）  system f">
<meta property="og:locale">
<meta property="og:image" content="https://vhuichen.github.io/2021/04/iOS/%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E6%9C%BA%E5%88%B6/%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E6%9C%BA%E5%88%B6/image-20220904111317704.png">
<meta property="og:image" content="https://vhuichen.github.io/2021/04/iOS/%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E6%9C%BA%E5%88%B6/%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E6%9C%BA%E5%88%B6/20210421192623.png">
<meta property="og:image" content="https://vhuichen.github.io/2021/04/iOS/%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E6%9C%BA%E5%88%B6/%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E6%9C%BA%E5%88%B6/image-20220904111357275.png">
<meta property="article:published_time" content="2021-04-19T16:00:00.000Z">
<meta property="article:modified_time" content="2022-09-04T03:14:37.806Z">
<meta property="article:author" content="vhuichen">
<meta property="article:tag" content="内存管理">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://vhuichen.github.io/2021/04/iOS/%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E6%9C%BA%E5%88%B6/%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E6%9C%BA%E5%88%B6/image-20220904111317704.png">


<link rel="canonical" href="https://vhuichen.github.io/2021/04/iOS/%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E6%9C%BA%E5%88%B6/%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E6%9C%BA%E5%88%B6/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-Hans","comments":true,"permalink":"https://vhuichen.github.io/2021/04/iOS/%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E6%9C%BA%E5%88%B6/%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E6%9C%BA%E5%88%B6/","path":"2021/04/iOS/内存管理机制/内存管理机制/","title":"iOS内存管理机制"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>iOS内存管理机制 | vhuichen</title>
  






  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">vhuichen</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">记录、分享</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section">Home</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section">Archives</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section">About</a></li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#0x00-%E5%86%85%E5%AD%98%E5%88%86%E7%B1%BB"><span class="nav-number">1.</span> <span class="nav-text">0x00 内存分类</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#Clean-Memory"><span class="nav-number">1.0.0.1.</span> <span class="nav-text">Clean Memory</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Dirty-Memory"><span class="nav-number">1.0.0.2.</span> <span class="nav-text">Dirty Memory</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#0x01-%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86"><span class="nav-number">2.</span> <span class="nav-text">0x01 内存管理</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#APP%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86"><span class="nav-number">2.1.</span> <span class="nav-text">APP内存管理</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Tagged-Pointer"><span class="nav-number">2.1.1.</span> <span class="nav-text">Tagged Pointer</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%B5%8B%E8%AF%95"><span class="nav-number">2.1.1.1.</span> <span class="nav-text">测试</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Non-pointer-iSA"><span class="nav-number">2.1.2.</span> <span class="nav-text">Non-pointer iSA</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#SideTables"><span class="nav-number">2.1.3.</span> <span class="nav-text">SideTables</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%B3%BB%E7%BB%9F%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86"><span class="nav-number">2.2.</span> <span class="nav-text">系统内存管理</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%B8%BA%E4%BB%80%E4%B9%88%E6%89%8B%E6%9C%BAAPP%E5%AE%B9%E6%98%93%E8%A2%AB%E7%B3%BB%E7%BB%9F%E6%9D%80%E6%AD%BB%EF%BC%8C%E7%94%B5%E8%84%91APP%E4%B8%8D%E4%BC%9A%E8%A2%AB%E6%9D%80%E6%AD%BB%EF%BC%8C%E4%BD%86%E5%8D%B4%E5%AE%B9%E6%98%93%E5%8D%A1%E6%AD%BB"><span class="nav-number">2.2.0.1.</span> <span class="nav-text">为什么手机APP容易被系统杀死，电脑APP不会被杀死，但却容易卡死</span></a></li></ol></li></ol></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="vhuichen"
      src="/images/vhuichen.jpg">
  <p class="site-author-name" itemprop="name">vhuichen</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">78</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">11</span>
        <span class="site-state-item-name">categories</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">86</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="Back to top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-Hans">
    <link itemprop="mainEntityOfPage" href="https://vhuichen.github.io/2021/04/iOS/%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E6%9C%BA%E5%88%B6/%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E6%9C%BA%E5%88%B6/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/vhuichen.jpg">
      <meta itemprop="name" content="vhuichen">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="vhuichen">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="iOS内存管理机制 | vhuichen">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          iOS内存管理机制
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-04-20 00:00:00" itemprop="dateCreated datePublished" datetime="2021-04-20T00:00:00+08:00">2021-04-20</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">Edited on</span>
      <time title="Modified: 2022-09-04 11:14:37" itemprop="dateModified" datetime="2022-09-04T11:14:37+08:00">2022-09-04</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">In</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/iOS/" itemprop="url" rel="index"><span itemprop="name">iOS</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="0x00-内存分类"><a href="#0x00-内存分类" class="headerlink" title="0x00 内存分类"></a>0x00 内存分类</h1><p>根据不同 Section 可以将内存分为</p>
<ul>
<li>代码段 .text</li>
<li>已初始化数据段 .data</li>
<li>未初始化数据段 .bss</li>
<li>堆 heap </li>
<li>栈 stack</li>
</ul>
<img src="/2021/04/iOS/%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E6%9C%BA%E5%88%B6/%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E6%9C%BA%E5%88%B6/image-20220904111317704.png" alt="image-20220904111317704" style="zoom:50%;">

<p>根据内存能否被系统回收，可以分为</p>
<ul>
<li>Clean Memory</li>
<li>Dirty Memory</li>
</ul>
<h4 id="Clean-Memory"><a href="#Clean-Memory" class="headerlink" title="Clean Memory"></a>Clean Memory</h4><p>内存紧张时可以被覆盖，下次需要使用时，触发缺页中断，然后从磁盘加载到内存 （Page In）</p>
<ul>
<li>system framework</li>
<li>binary executable of your app</li>
<li>memory mapped files</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">疑问：链接的 framework 中 _DATA_CONST 并不绝对属于 clean memory，当 app 使用到 framework 时，就会变成 dirty memory。</span><br><span class="line">这里，嗯，没理解什么意思</span><br></pre></td></tr></table></figure>

<h4 id="Dirty-Memory"><a href="#Dirty-Memory" class="headerlink" title="Dirty Memory"></a>Dirty Memory</h4><p>无法被系统回收的内存，内存紧张时会给进程发送通知，需要程序手动释放这部分内存。同时系统会压缩这部分的内存，等下次使用时再解压。</p>
<ul>
<li>heap allocation</li>
<li>caches</li>
<li>decompressed images</li>
<li>compressed memory</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">虚拟内存</span><br><span class="line">Virtual Memory = Clean Memory + Dirty Memory  </span><br><span class="line">物理内存</span><br><span class="line">Resident Memory = Clean Memory(Loaded in Physical Memory) + Dirty Memory  </span><br><span class="line">实际内存占用</span><br><span class="line">memory footprint = dirty size + compressed size</span><br></pre></td></tr></table></figure>

<h1 id="0x01-内存管理"><a href="#0x01-内存管理" class="headerlink" title="0x01 内存管理"></a>0x01 内存管理</h1><p>内存管理可以分为两部分  </p>
<ul>
<li>APP内存管理，由APP内部控制</li>
<li>系统内存管理，由系统控制</li>
</ul>
<h2 id="APP内存管理"><a href="#APP内存管理" class="headerlink" title="APP内存管理"></a>APP内存管理</h2><p>APP管理方案有3中</p>
<h3 id="Tagged-Pointer"><a href="#Tagged-Pointer" class="headerlink" title="Tagged Pointer"></a>Tagged Pointer</h3><p>在64位的机器上，未引入 Tagged Pointer 之前内存结构如下图，对于一些很小的数据，在64位的机器下占用的内存翻了一倍，单单是指针就占用了2&#x2F;3的字节，同时还要在堆分配内存，维护引用计数等<br><img src="/2021/04/iOS/%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E6%9C%BA%E5%88%B6/%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E6%9C%BA%E5%88%B6/20210421192623.png" width="500"> </p>
<p>基于以上问题，苹果引入了 Tagged Pointer 对象，把一个对象的指针分为两部分，一部分作为数据标识，一部分存储数据。此时对象的指针不再是指针，更像是一个变量，并且不需要在堆中分配内存，这样不仅减少了占用内存，还提高了使用效率。<br><img src="/2021/04/iOS/%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E6%9C%BA%E5%88%B6/%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E6%9C%BA%E5%88%B6/image-20220904111357275.png" alt="image-20220904111357275" style="zoom:40%;"></p>
<p>Tagged Pointer 对象会在使用时创建，存放在栈区，同一个值每次创建都是同一个地址。<br>iOS默认开启了 Tagged Pointer 混淆，调试时设置 <code>OBJC_DISABLE_TAG_OBFUSCATION = YES</code> 后，数据正常了，每次APP启动后，都是同一个值</p>
<h4 id="测试"><a href="#测试" class="headerlink" title="测试"></a>测试</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">NSNumber *num1 = @(7); //0x8000000000000393</span><br><span class="line">NSNumber *num2 = @(3); //0x8000000000000193</span><br></pre></td></tr></table></figure>
<p>低3位表示类标识<br>低4~7位表示数据类型<br>最高位表示是否是Tagged Pointer  </p>
<h3 id="Non-pointer-iSA"><a href="#Non-pointer-iSA" class="headerlink" title="Non-pointer iSA"></a>Non-pointer iSA</h3><p>在64位的架构下，指针查找数据并不需要64位，而苹果实际上只用33位来存储地址，剩下的用来存储一些其他的数据，iSA指针的结构如下  </p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">// 注意真机和模拟器的结构是不一样的</span><br><span class="line">union isa_t </span><br><span class="line">&#123;</span><br><span class="line">    Class cls;</span><br><span class="line">    uintptr_t bits;</span><br><span class="line">    </span><br><span class="line">#   define ISA_MASK        0x0000000ffffffff8ULL</span><br><span class="line">#   define ISA_MAGIC_MASK  0x000003f000000001ULL</span><br><span class="line">#   define ISA_MAGIC_VALUE 0x000001a000000001ULL</span><br><span class="line">    struct &#123;</span><br><span class="line">        uintptr_t nonpointer        : 1; //是否开启 nonpointer </span><br><span class="line">        uintptr_t has_assoc         : 1; //关联对象</span><br><span class="line">        uintptr_t has_cxx_dtor      : 1; //表明对象是否有C++或ARC析构函数</span><br><span class="line">        uintptr_t shiftcls          : 33; // MACH_VM_MAX_ADDRESS 0x1000000000</span><br><span class="line">        uintptr_t magic             : 6;</span><br><span class="line">        uintptr_t weakly_referenced : 1; //弱引用</span><br><span class="line">        uintptr_t deallocating      : 1;</span><br><span class="line">        uintptr_t has_sidetable_rc  : 1;  //是否当前的引用计数过大</span><br><span class="line">        uintptr_t extra_rc          : 19; // 引用计数 = extra_rc + 1，超过就通过 SideTable 存储</span><br><span class="line">    &#125;;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>是否使用 Non-pointer iSA 由苹果决定</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">1：包含swift代码</span><br><span class="line">2：sdk版本低于10.11</span><br><span class="line">3：runtime读取image时发现这个image包含__objc_rawisa段</span><br><span class="line">4：开发者自己添加了OBJC_DISABLE_NONPOINTER_ISA=YES到环境变量中</span><br><span class="line">5：某些不能使用Non-pointer的类，GCD等</span><br><span class="line">6：父类关闭</span><br></pre></td></tr></table></figure>

<h3 id="SideTables"><a href="#SideTables" class="headerlink" title="SideTables"></a>SideTables</h3><p>SideTables 是一个散列表， 用来管理对象的引用计数和弱引用。由于对象引用计数的操作是原子性操作所以 SideTable 中使用了自旋锁，SideTables 分成了8个 SideTable，实现了分离锁技术，提高了效率。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">struct SideTable &#123;</span><br><span class="line">	//非公平的自旋锁</span><br><span class="line">	spinlock_t slock;</span><br><span class="line">	//强引用相关，内部是一个hash表。</span><br><span class="line">	RefcountMap refcnts;</span><br><span class="line">	//弱引用相关，内部也是一个哈希表，每一个元素指向一个可变数组</span><br><span class="line">	weak_table_t weak_table;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li><code>RefcountMap</code> 仅在未开启 isa 优化或 isa 优化情况下的引用计数溢出时才会用到  </li>
<li>8个 SideTable 可以一定程度上解决效率问题</li>
</ul>
<h2 id="系统内存管理"><a href="#系统内存管理" class="headerlink" title="系统内存管理"></a>系统内存管理</h2><p>当系统发现没有可用的内存页时，可能会有以下步骤  </p>
<ul>
<li>覆盖掉优先级较低的 Clean Memory ，以页为单位</li>
<li>给所有的前后台APP进程发送内存警告通知（一般APP会释放掉一些可以再次加载的内存）</li>
<li>通过上面两个步骤后，内存依然不够用，低内存管理机制 Jetsam 会根据优先级 kill 对应的进程</li>
</ul>
<h4 id="为什么手机APP容易被系统杀死，电脑APP不会被杀死，但却容易卡死"><a href="#为什么手机APP容易被系统杀死，电脑APP不会被杀死，但却容易卡死" class="headerlink" title="为什么手机APP容易被系统杀死，电脑APP不会被杀死，但却容易卡死"></a>为什么手机APP容易被系统杀死，电脑APP不会被杀死，但却容易卡死</h4><ul>
<li>电脑的 Swap 区在硬盘中，硬盘本身很大，很轻松的就虚拟出一个内存（虚拟内存）。机械硬盘不限读写次数，所以内存和硬盘之间可以无限读写。  </li>
<li>手机一般是用 flash 做存储器的，读写次数有限，如果用 flash 做 Swap 区，那么 flash 很可能在短时间内报废。所有手机一般都无 Swap 区。原因：1. flash 大小有限。2. flash 读写次数有限制</li>
</ul>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86/" rel="tag"># 内存管理</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2021/04/NetworkProtocol/HTTP%E5%8D%8F%E8%AE%AE/" rel="prev" title="HTTP协议">
                  <i class="fa fa-chevron-left"></i> HTTP协议
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2021/04/iOS/Allocations/Allocations/" rel="next" title="Instruments 之 Allocations">
                  Instruments 之 Allocations <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 2015 – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">vhuichen</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/pisces/" rel="noopener" target="_blank">NexT.Pisces</a>
  </div>

    </div>
  </footer>

  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script>

  





  





</body>
</html>
